package com.sinodata.bsm.cicp.finder.linux;

/**
 * 文件系统发现程序.详情查看"linux监控功能说明.xls"
 * @author zhaoch
 * @version 1.0
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class LinuxFS extends LinuxFinder {

    private static final Logger logger = Logger.getLogger(LinuxFS.class);
    //    private static final String strPattern = "(/dev/sda\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%\\s+(.*)";
    private static final String strPattern = "(\\S+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)%\\s+(.*)";
    //private static final String CMDSTR = "df -k ";
    private static final String CMDSTR = "df -k|awk '{ if(NF==1) printf(\"%s\", $0); else print $0;}' ";

    public static void main(String[] args) {

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String[] cpuFS = null;
        List<String[]> list = new ArrayList<String[]>();
        String strLine = null;
        BufferedReader reader = null;
        Pattern pattern = Pattern.compile(strPattern);
        try {
            reader = CommandReader.getReader(ip, CMDSTR, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(strLine);
                if (matcher.find()) {
                    /*
                     * 每一个String[]表示一个资源数据，String[]数据定义如下：
                     * 第一个元素：资源的内部标示
                     * 第二个元素：资源名称
                     * 第三个元素：资源描述
                     * 第四-第N个参数：返回的属性值
                     */
                    cpuFS = new String[5];
                    list.add(cpuFS);
                    cpuFS[0] = matcher.group(6).trim();
                    cpuFS[1] = matcher.group(6).trim();
                    cpuFS[2] = super.getHostName(ip) + " FS " + cpuFS[1];
                    cpuFS[3] = matcher.group(1);
                    cpuFS[4] = matcher.group(6);
                }
            }
        } catch (IOException e) {
            logger.error("failed to find LinuxFS", e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                logger.error("failed to close reader", e);
            }
        }
        return list;
    }
}
